<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace app\controller\adminapi\platform\free;

use app\common\services\table\Column;
use app\common\services\table\DataForm;
use app\common\services\table\DataTable;
use app\common\model\platform\PlatformAddon;
use app\common\controller\PlatformController;

/**
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 * Created on:  2025/5/13 20:49
 */
class Addon extends PlatformController
{
    /**
     * @var PlatformAddon
     */
    protected $model;


    // 设置关联查询
    protected array $withJoinTable = ['addons'];

    // 设置快捷搜索字段
    protected string|array $quickSearchField = [];

    /**
     * 控制器初始化
     * Create by :  刘泓宾<514660094@qq.com>
     * @return void
     */
    public function initialize(): void
    {
        parent::initialize();
        $this->model = new PlatformAddon();
        $this->model->plaid = get_plaid();
        // 判断请求控制器方法
        if ($this->request->action() == "index" && !$this->request->param("select")) {
            // 定义全局的查询范围带有alias
            $this->model->setGlobalScope();
        }
    }

    /**
     * 数据表格
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function table()
    {
        $DataTable = (new DataTable($this->model));
        if ($this->request->param('recycle')) {
            // 回收站
            $DataTable->field([$this->model->getPk(), 'delete_time']);
        }
        $button = Column::make()->demoButton;
        $button['type'] = 'warning';
        // 按钮的属性参数
        $button['attr'] = [
            'style' => 'font-size:20px;',
            'size'  => 'large',
        ];
        $settingButton = array_merge($button, ['title' => '系统设置', "text" => "", 'icon' => 'Tools', 'name' => 'setting', 'type' => 'primary']);
        $jumpButton = array_merge($button, ['title' => '跳转访问', "text" => "", 'icon' => 'Promotion', 'name' => 'jump', 'type' => 'danger']);
        $column = $DataTable
            ->setHideFields(['aid', 'addon_name'])
            ->setFilterFields(['create_time', 'update_time', 'visit'])
            ->setField('index', ['render' => 'switch'])
            ->setField('menu', ['render' => 'switch'])
            ->setField('token', ['render' => 'copy', 'label' => '通讯秘钥'])
            ->setAfterFields([
                'id' =>
                    [
                        'slotName'       => 'addon_solt',
                        'label'          => '应用',
                        'prop'           => 'addons.title',
                        'operator'       => 'LIKE',
                        'comSearchLabel' => '应用名称',
                        'operatorPlaceholder'=>'输入应用名称'
                    ]
            ])
            ->operation(
                Column::make()->prop('operation')
                    // 操作列标题
                    ->label('操作')
                    ->render('buttons')
                    // 宽度
                    ->width(100)
                    ->other('fixed', 'right')
                    // 禁用搜索
                    ->operator(false)
                    // 自定义按钮组
                    ->buttons([
                        $settingButton,
                        $jumpButton,
                        //                               "edit", "del"
                    ])
                    ->create()
            )
            ->create();
        return $this->success('ok', ['column' => $column, 'pk' => $this->model->getPk()]);
    }

    /**
     * 数据表单
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function form()
    {
        $dataForm = new DataForm($this->model);
        $form = $dataForm
            ->setFilterFields(['addon_name', 'aid'])
            ->setField('index', ['render' => 'switch'])
            ->setField('menu', ['render' => 'switch'])
            ->create();
        return $this->success('', $form);
    }

    /**
     * 获取数据列表
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     * @throws \Throwable
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        if ($this->request->param('select')) {
            return $this->select();
        }
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->append(['addon'])
            ->paginate($limit);

        return $this->success('', [
            'list'  => $res->items(),
            'total' => $res->total(),
        ]);
    }
}
